package com.lovo.roomProject.classManager.frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoCutPage;
import com.lovo.netCRM.component.LovoTable;
import com.lovo.netCRM.component.LovoTitleLabel;
import com.lovo.roomProject.classManager.service.ClassService;
import com.lovo.roomProject.classManager.service.impl.ClassServiceImpl;
import com.lovo.roomProject.pojo.CutPageBean;

/**
 * 宿舍管理系统
 *
 * @author 张成峰
 * @version 1.0
 * @description 班级管理面板
 * @see
 */
public class ClassManagerPanel extends JPanel {
    /**
     * 班级表格组件
     */
    private LovoTable classTable;
    /**
     * 窗体对象
     */
    private JFrame jf;

    public ClassManagerPanel(JFrame jf) {
        this.jf = jf;
        this.setLayout(null);
        this.init();
    }

    /**
     * 服务层对象
     */
    private ClassService classService = new ClassServiceImpl();

    /**
     * 分页组件
     */
    private LovoCutPage lovoCutPage = new LovoCutPage(30, 400, this) {
        @Override
        public void clickEvent(int current) {
            findClasz(current);
        }
    };

    /**
     * 初始化
     */
    private void init() {
        new LovoTitleLabel("班 级 管 理", this);
        this.initTable();
        this.initButton();
        this.initData();
    }

    /**
     * 初始化数据
     */
    public void initData() {
        this.updateClassTable();
    }

    /**
     * 初始化按钮
     */
    private void initButton() {
        LovoButton addButton = new LovoButton(200, 500, "添加班级", "image/add.png", this);
        addButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                new ClassAddDialog(jf, ClassManagerPanel.this);
            }
        });


        LovoButton delButton = new LovoButton(400, 500, "删除班级", "image/del.png", this);
        delButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                int key = classTable.getKeyByInt();
                if (key == -1) {
                    JOptionPane.showMessageDialog(null, "请选择行");
                    return;
                }
                int confirm = JOptionPane.showConfirmDialog(null, "确定删除班级？");
                if (confirm == 0) {
                    try {
                        delClass(key);
                    } catch (Exception exception) {
                        JOptionPane.showMessageDialog(null,exception.getMessage());
                    }
                }

            }
        });
    }

    //--------------------------

    /**
     * 初始化表格
     */
    private void initTable() {
        classTable = new LovoTable(this,
                new String[]{"班级名称", "开班时间", "带班老师"},
                new String[]{"name", "createDate", "teacher"},//班级实体属性名数组 new String[]{"name","createTime"}
                "id");//主键属性名 classId
        classTable.setSizeAndLocation(20, 90, 700, 300);

    }

    /**
     * 更新表格数据
     */
    private void updateClassTable() {
//		List list = classService.listClass();
        //更新表格,插入班级List集合
        findClasz(1);

//		classTable.updateLovoTable(list);
    }

    /**
     * 删除班级
     *
     * @param classId 班级ID
     */
    private void delClass(int classId) throws Exception{
        //删除班级
        classService.delete(classId);
        //更新表格
        this.updateClassTable();
    }

    /**
     * 查找学班级
     */
    private void findClasz(int current) {
        CutPageBean cutPageBean = classService.listClassByPage(current);
        //更新表格,显示查询结果
        classTable.updateLovoTable(cutPageBean.getList());
        if (current == 1) {
            lovoCutPage.setTotalPage(cutPageBean.getTotalPage());
        }
    }
}
